package workerwithdb.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import workerwithdb.managers.SortOrdersBy;
import api.entitites.IMaster;
import api.entitites.IOrder;
import api.entitites.OrderStatus;

public class HibernateDAOOrder extends GenericDAOHibernate<IOrder> {

	private final String ORDER_STATUS = "orderstatus";
	private final String BEGINING_DATE = "beginingdate";
	private final String ORDER = "order";
	private final String NUMBER = "number";
	private final String DEADLINE_DATE = "deadlinedate";
	
	
	public HibernateDAOOrder(){
		super(IOrder.class);
	}
	
	public void insertAllOrders(ArrayList<IOrder> ordersList,Session session){
		for(IOrder o:ordersList){
			super.saveOrUpdate(o, session);
		}
	}
	
	public IOrder readOrder(Integer orderNumber,Session session){
		return  (IOrder)super.get(NUMBER, orderNumber,session);
	}
	
	public List<IOrder> sortAllOrders(SortOrdersBy sortBy,Session session) {
		Criteria criteria = session.createCriteria(IOrder.class).addOrder(Order.asc(sortBy.toString().toLowerCase()));
		@SuppressWarnings("unchecked")
		List<IOrder> sortOrderList = criteria.list();
		return sortOrderList;
	}

	public List<IOrder> sortMadingOrders(SortOrdersBy sortBy,Session session) {
		Criteria criteria = session.createCriteria(IOrder.class).add(Restrictions.isNull(ORDER_STATUS)).addOrder(Order.asc(SortOrdersBy.BEGININGDATE.toString().toLowerCase()));
		@SuppressWarnings("unchecked")
		List<IOrder> madingOrdersList = criteria.list();
		return madingOrdersList;
		
	}

	public List<IOrder> sortOrdersByPeriod(Date begin, Date end,SortOrdersBy sortBy, OrderStatus orderstatus,Session session) {
		Criteria criteria = session.createCriteria(IOrder.class).add(Restrictions.eq(ORDER_STATUS, orderstatus)).add(Restrictions.between(BEGINING_DATE, begin,end)).addOrder(Order.asc(sortBy.toString().toLowerCase()));
		@SuppressWarnings("unchecked")
		List<IOrder> ordersListByPeriod = criteria.list();
		return ordersListByPeriod;
		
	}
	
	
	public IMaster getMasterByOrder(Integer orderNumber,Session session){
		Criteria criteria = session.createCriteria(IMaster.class).setFetchMode(ORDER, FetchMode.JOIN).createCriteria(ORDER).add(Restrictions.eq(NUMBER,orderNumber));
		IMaster master = (IMaster) criteria.uniqueResult();
		return master;
		
	}
	
	
	
	public Date getNextFreeDate(Session session){
		Criteria criteria = session.createCriteria(IOrder.class).setProjection(Projections.min(DEADLINE_DATE)).add(Restrictions.isNull(ORDER_STATUS));
		Date nextDate = (Date) criteria.uniqueResult();
		return nextDate;	
		
	}
	
	
	public Long getMaxNumberOrder(Session session){
		Criteria criteria = session.createCriteria(IOrder.class).setProjection(Projections.max(NUMBER));
		Long numberOrder = (Long) criteria.uniqueResult();
		return numberOrder;
		
	}
	
	
	
	
	
}








